package com.example.demo.controller;

import com.example.demo.dto.UserDto;
import com.example.demo.enty.Order;
import com.example.demo.enty.Stu;
import com.example.demo.enty.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/jpa")
    public List<User> findAll(){
        return userService.findAll();
    }

    @RequestMapping("/page")
    public List<User> findByPage(
            @RequestParam("size") Integer size,
            @RequestParam("page") Integer page
    ){

        return userService.findByPage(size,page);

    }
    @GetMapping("/page")
    public List<User> findByPage2(
            @RequestParam("size") Integer size,
            @RequestParam("page") Integer page,
            @RequestParam("name") String name
    ){

        return userService.findByPage2(name,size,page);

    }

    @PutMapping
    public void save(){
        userService.save();
    }

    @GetMapping("/findd")
    public User findd(){
        User alld = userService.findAlld(new User(1L, "", 1));
        return alld;
    }

    @GetMapping("/mohu")
    public List<User> mohu(){
        List<User> userList=userService.findBymohu();
        return userList;
    }

    @GetMapping("/pages")
    public List<User> findByPages(){
        List<User> list=userService.findByPages();
        return list;
    }
    @GetMapping("/all")
    public List<User> findAllJpaDsl(){
        List<User> userList=userService.findAllJpaDsl();
        return userList;
    }
    @GetMapping("/orderAll")
    public List<Order> findAllJpaDsl1(){
        List<Order> orderList=userService.findAllJpaDsl1();
        return orderList;
    }
    @GetMapping("/condition/{name}")
    public List<User> findBytioajian(

            @PathVariable("name")String name
    ){
        List<User> userList=userService.findBytioajian(name);
        return userList;
    }

    //根据id获取
    @GetMapping("/{id}")
    public User findById(@PathVariable("id")Long id){
        return userService.findById(id);
    }
    //单表多条件查询
    @GetMapping("/manyCondition")
    public List<User> findByManyCondition(
            @RequestParam("name")String name,
            @RequestParam("age")Integer age
    ){
        return userService.findByManyCondition(name,age);
    }

    //使用join进行查询
    @GetMapping("/join")
    public List<User> findAllByJoin(){
        return userService.findAllByJoin();
    }
    //更新
    @PostMapping("/update")
    public void update(
            @RequestParam("id")Long id,
            @RequestParam("name")String name
    ){
        userService.update(name,id);

    }
    //分页查询所有字段，再根据index字段排序
    @GetMapping("/pageDSL")
    public List<User> findByPageDsl(
            @RequestParam("page")Integer page,
            @RequestParam("size")Integer size
            ){
        return userService.findByPageDsl(page,size);

    }
    //部分字段映射查询
    @GetMapping("/mapper")
    public List<UserDto> findByMapper(
            @RequestParam("page")Integer page,
            @RequestParam("size")Integer size
    ){
        return userService.findByMapper(page,size);
    }
    //多表查询
    @GetMapping("/manyTable")
    public List<Order> findByOrder(){
        return userService.findByOrder();
    }
    //多表查询
    @GetMapping("/stu")
    public List<Stu> findByStu(){
        return userService.findByStu();
    }

}
